package com.admin.service.impl;

import com.admin.common.enums.DeclareLogTypeEnum;
import com.admin.common.exception.BizException;
import com.admin.common.shiro.ShiroUtils;
import com.admin.common.util.BigDecimalUtil;
import com.admin.common.util.DateUtils;
import com.admin.domain.*;
import com.admin.mapper.*;
import com.admin.req.score.ScoreItemModuleReq;
import com.admin.req.score.ScoreReq;
import com.admin.service.DeclareLogService;
import com.admin.service.DeclareScoreService;
import com.admin.vo.ResultVO;
import com.admin.vo.declareDetail.log.DeclareLogVO;
import com.admin.vo.declareDetail.score.ScoreListVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author yangzhongliang
 * @date 2022/05/16
 */
@Slf4j
@Service
public class DeclareLogServiceImpl implements DeclareLogService {
    @Autowired
    private DeclareMapper declareMapper;
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private DeclareScoreService declareScoreService;
    @Autowired
    private DeclareScoreMapper declareScoreMapper;
    @Autowired
    private DeclareScoreModuleMapper declareScoreModuleMapper;
    @Autowired
    private ScoreParamMapper scoreParamMapper;

    @Autowired
    private DeclareLogMapper declareLogMapper;


    @Override
    public void save(AdminDO adminDO, Integer declareId, String type, String content, String remark) {
        DeclareLog declareLog = new DeclareLog();
        declareLog.setDeclareId(declareId);
        declareLog.setOperatorType(type);
        declareLog.setContent(content);
        declareLog.setRemark(remark);
        declareLog.setUserLevel(adminDO.getMemberLevel());
        declareLog.setUserLevelName("浙江省");
        declareLog.setOperatorId(adminDO.getUserId());
        declareLog.setOperatorMobile(adminDO.getMobile());
        declareLog.setOperatorName(adminDO.getRealname());
        declareLogMapper.insertSelective(declareLog);
    }


    @Override
    public void insertLog(DeclareLog declareLog) {
        AdminDO adminDO = adminMapper.selectByPrimaryKey(ShiroUtils.getUserId());
        declareLog.setOperatorId(adminDO.getUserId());
        declareLog.setOperatorMobile(adminDO.getMobile());
        declareLog.setOperatorName(adminDO.getRealname());
        declareLogMapper.insertSelective(declareLog);
    }

    @Override
    public List<DeclareLogVO> findByDeclareId(Integer declareId) {
        Map<String,Object> params = new HashMap<>();
        params.put("declareId", declareId);
        List<DeclareLog> list = declareLogMapper.selectByMap(params);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        List<DeclareLogVO> listVOList = new ArrayList<>();
        for (DeclareLog declareLog : list) {
            DeclareLogVO listVO = new DeclareLogVO();
            BeanUtils.copyProperties(declareLog, listVO);
            //操作时间
            listVO.setCreateTimeStr(DateUtils.format(declareLog.getCreateTime(), DateUtils.DATE_PATTERN_TIME));
            listVO.setOperatorTypeStr(DeclareLogTypeEnum.getText(declareLog.getOperatorType()));
            listVOList.add(listVO);
        }
        return listVOList;
    }

    @Override
    public int deleteByDeclareId(Integer declareId) {
        return declareLogMapper.deleteByDeclareId(declareId);
    }
}
